package ru.cod.rs.managers;

import ru.cod.rs.views.GameView;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

/**
 * Abstract Manager for rendering
 * 
 * @author Suslov Vadim
 * 
 */
public abstract class DrawManager extends AbstractManager {

	protected GameView view;
	protected SurfaceHolder holder;

	public DrawManager(Context c, GameView v) {
		super(c);
		view = v;
		holder = view.getHolder();
	}

	// /////////////////////////////////////////////
	@Override
	protected void execute() {
		Canvas canvas = null;
		try {
			canvas = holder.lockCanvas();
			update();
			render(canvas);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (canvas != null) {
				holder.unlockCanvasAndPost(canvas);
			}
		}
	}

	// /////////////////////////////////////////////

	protected abstract void update();

	protected abstract void render(Canvas c);

	// ////////////////////////////////////////////

	protected Bitmap getImage(int id) {
		return BitmapFactory.decodeResource(context.getResources(), id);
	}
}
